#region

using System;
using System.Collections.Specialized;
using System.Web;
using BuildMotion.Framework.Logging;
using BuildMotion.MotionTracker.Common;
using BuildMotion.MotionTracker.Properties;
using BuildMotion.MotionTracker.Provider;

#endregion

namespace BuildMotion.MotionTracker
{
	/// <summary>
	/// The implemented Notification business provider. This is the MotionTracker business vertical.
	/// </summary>
	public class SqlMotionTrackerProvider : MotionTrackerProvider, IMotionTrackerProvider
	{
		#region Fields;;

		private string applicationName;
		private string name;

		#endregion

		#region Provider Methods;

		/// <summary>
		/// Tracks the request.
		/// </summary>
		/// <param name="application">The application.</param>
		/// <param name="trackerId">The tracker id.</param>
		/// <param name="originId">The origin id.</param>
		public override void TrackRequest(HttpApplication application, Guid trackerId, Guid originId)
		{
			MotionTrackerRepository repository = new MotionTrackerRepository();
			repository.TrackRequest(application, trackerId.ToString(), originId.ToString());
		}

		/// <summary>
		/// Use to get the application name from the Provider.
		/// </summary>
		/// <returns>Returns the name of the application.</returns>
		public override string GetApplicationName()
		{
			return applicationName;
		}

		public override void TrackRequest(HttpApplication application)
		{
			//TODO: Implement actions for the MotionTracker provider;
			MotionTrackerRepository repository = new MotionTrackerRepository();
			repository.TrackRequest(application);
		}

		/// <summary>
		/// Use to initialize the Notification provider.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="config"></param>
		public override void Initialize(string name, NameValueCollection config)
		{
			try
			{
				if(config == null)
				{
					throw new Exception(Resources.ConfigurationFileIsNullOrInvalid);
				}

				if(String.IsNullOrEmpty(name))
				{
					name = "SqlMotionTrackerProvider";
				}

				if(string.IsNullOrEmpty(config["description"]))
				{
					config.Remove("description");
					config.Add("description", string.Empty);
				}

				base.Initialize(name, config);

				this.name = name;

				#region Configure Provider Attributes;

				applicationName = config["applicationName"];
				if(string.IsNullOrEmpty(applicationName))
				{
					throw new Exception(Resources.ArgumentIsNull);
				}

				if(applicationName.Length > 256)
				{
					throw new Exception(Resources.ApplicationNameExceedsLength);
				}

				#endregion

				#region Configuration Removal of Provider Attributes;

				config.Remove("applicationName");
				config.Remove("name");

				if(config.Count > 0)
				{
					string attribUnrecognized = config.GetKey(0);
					if(!String.IsNullOrEmpty(attribUnrecognized))
					{
						throw new Exception(Resources.ProviderHasUnrecognizedAttribute);
					}
				}

				#endregion
			}
			catch(Exception ex)
			{
				bool rethrow = ExceptionLogger.Log(ex, Resources.ExceptionLogPolicyName);
				if(rethrow)
				{
					throw;
				}
			}
		}

		#endregion

		#region Utility Methods

		#endregion

		#region Properties;

		/// <summary>
		/// The name of the application that is configured in the Notification provider web.config section.
		/// </summary>
		public override string ApplicationName
		{
			get
			{
				return applicationName;
			}
			set
			{
				applicationName = value;
			}
		}

		///// <summary>
		///// Saves the tag.
		///// </summary>
		///// <param name="tagContext">The tag context.</param>
		///// <returns></returns>
		//public override TagContext SaveTag(TagContext tagContext)
		//{
		//    //SaveTagAction action = new SaveTagAction(tagContext);
		//}

		#endregion;
	}
}